package com.example.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.openapi.model.ApiResponse;
import com.example.openapi.service.WaiqinApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 勤策OpenAPI控制器
 *
 * @author MCP Service
 * @version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/qince")
public class WaiqinApiController {

    @Autowired
    private WaiqinApiService waiqinApiService;

    /**
     * 通用API调用接口
     *
     * @param module    模块名称（如：organization）
     * @param version   版本号（如：v1）
     * @param operation 操作名称（如：modifyOrganization）
     * @param params    请求参数
     * @return API响应
     */
    @PostMapping("/call")
    public ApiResponse<JSONObject> callApi(
            @RequestParam String module,
            @RequestParam String version,
            @RequestParam String operation,
            @RequestBody(required = false) Map<String, Object> params) {
        try {
            log.info("接收到API调用请求 - Module: {}, Version: {}, Operation: {}, Params: {}",
                    module, version, operation, params);
            JSONObject result = waiqinApiService.callApi(module, version, operation, params);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("API调用失败", e);
            return ApiResponse.error("API调用失败: " + e.getMessage());
        }
    }

    /**
     * 健康检查接口
     *
     * @return 服务状态
     */
    @GetMapping("/health")
    public ApiResponse<String> health() {
        return ApiResponse.success("勤策OpenAPI服务运行正常");
    }
}
